﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BridController : MonoBehaviour {

    // rigidbody 替我们实现类似现实的物理交互

    // 下落，惯性，力学，加速度
    private Rigidbody2D rigidbody;

    public float jumpPower = 9.0f;

    // 死亡flag
    public bool isDead = false;

    // 动画控制器
    Animator animator;

    // 会杀死小鸟的敌人名称
    String[] enemies = new String[2] {
        "wall",
        "floor"
    };

    void Start () {

        // 动态获取自身组件
        this.rigidbody = this.gameObject.GetComponent<Rigidbody2D> ();
        this.animator = gameObject.GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update () {

        Debug.Log ("bird");
        // 会被Rigidbody2D（刚体组件自动执行）
        //this.transform.position += rigidbody.velocity;
        Jump ();

        // 切换图片的事件
        // 第一帧 站立
        // 第二帧 迈左脚
        // 第三帧 收左脚

    }

    void Jump () {

        // 死掉的话禁止输入事件
        if (Input.GetKeyDown (KeyCode.Space) && !isDead) {
            Debug.Log ("isJump");
            // 重力加速停止
            rigidbody.velocity = new Vector2 (0, 7.2f);
        }
    }

    // 与其他物体碰撞发生时
    void OnCollisionEnter2D (Collision2D other) {

        if (other.gameObject.name.Contains (enemies[0]) || other.gameObject.name.Contains (enemies[1])) {

            // 判定其为死亡
            this.isDead = true;
            GameManaer.instance.isGameOver = true;
            // GameControl.instance.BirdDied();
            //播放死亡动画
            this.animator.SetBool ("isDead", this.isDead);
        }
    }
    void OnTriggerEnter2D (Collider2D other) {




        if (other.gameObject.name.Contains ("scorePoint")) {
            GameManaer.instance.score += 1;

            Debug.Log (GameManaer.instance.score);
        }

    }

}